package com.ningmengs789.live.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ningmengs789.live.R;
import com.ningmengs789.live.live.live.common.widget.gift.utils.OnItemClickListener;
import com.ningmengs789.live.net.response.CountryBean;

import java.util.ArrayList;

public class CountryAdapter extends RecyclerView.Adapter<CountryAdapter.Vh> {

    private ArrayList<CountryBean> mList;
    private LayoutInflater mInflater;
    private View.OnClickListener mOnClickListener;
    private OnItemClickListener<CountryBean> mOnItemClickListener;
    private String mobileCode = "";
    private Context context;

    public CountryAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        mOnClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Object tag = v.getTag();
                if (tag != null) {
                    int position = (int) tag;
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(mList.get(position), position);
                    }
                }
            }
        };
    }

    public void setOnItemClickListener(OnItemClickListener<CountryBean> onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public void addData(ArrayList<CountryBean> list, String mobile_code){
        this.mList = list;
        this.mobileCode = mobile_code;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public Vh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Vh(mInflater.inflate(R.layout.item_live_country, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull Vh vh, int position) {
        vh.setData(mList.get(position), position);
    }

    @Override
    public int getItemCount() {
        if (mList == null){
            return 0;
        }
        return mList.size();
    }

    class Vh extends RecyclerView.ViewHolder {

        private TextView mLetter;
        private TextView mName;
        private ImageView mIcon;

        public Vh(View itemView) {
            super(itemView);
            mLetter = itemView.findViewById(R.id.letter);
            mName = itemView.findViewById(R.id.name);
            mIcon = itemView.findViewById(R.id.icon);
            itemView.setOnClickListener(mOnClickListener);
        }

        void setData(CountryBean bean, int position) {
            itemView.setTag(position);
            mName.setText(bean.getCountry_name());

            if (mobileCode.equals(bean.getMobile_code())){
                mIcon.setVisibility(View.VISIBLE);
//                mName.setTextColor(context.getResources().getColor(R.color.color_style));
            }else {
//                mName.setTextColor(context.getResources().getColor(R.color.color_title_txt));
                mIcon.setVisibility(View.GONE);
            }

            if (bean.getLetter() != null){
                mLetter.setVisibility(View.VISIBLE);
                mLetter.setText(bean.getLetter());
            }else {
                mLetter.setVisibility(View.GONE);
            }
        }
    }
}
